package org.ala.tiktools.rpc.config;

import java.util.Arrays;
import java.util.stream.Collectors;

import org.ala.tiktools.rpc.client_server.service.IRpcServiceReceiver;
import org.ala.tiktools.rpc.plugin.load_balance.ILoadBalance;
import org.ala.tiktools.rpc.plugin.load_balance.selector.ISelector;
import org.ala.tiktools.rpc.plugin.node.INodeCreator;
import org.ala.tiktools.rpc.plugin.serializer.ISerializer;
import org.springframework.core.env.Environment;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

/**
 * @author ala
 * @date 2025-04-05 22:36
 */
public class RpcConfigInfoParse {
    /**
     *  前缀
     */
    public static final String PREFIX = "ala-rpc";


    /**
     *  解析配置类
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public RpcConfigInfo parse(Environment env) throws ClassNotFoundException {
        RpcConfigInfo c = new RpcConfigInfo();

        String loadBalanceClass = env.getProperty(PREFIX + ".loadBalanceClass", String.class);
        c.setLoadBalanceClass((Class<? extends ILoadBalance>) ClassUtils.forName(loadBalanceClass, env.getClass().getClassLoader()));

        String nodeCreatorClass = env.getProperty(PREFIX + ".nodeCreatorClass", String.class);
        c.setNodeCreatorClass((Class<? extends INodeCreator>) ClassUtils.forName(nodeCreatorClass, env.getClass().getClassLoader()));

        String nodeSelectorClass = env.getProperty(PREFIX + ".nodeSelectorClass", String.class);
        c.setNodeSelectorClass((Class<? extends ISelector>) ClassUtils.forName(nodeSelectorClass, env.getClass().getClassLoader()));

        String serviceReceiverClass = env.getProperty(PREFIX + ".serviceReceiver", String.class);
        c.setServiceReceiverClass((Class<? extends IRpcServiceReceiver>) ClassUtils.forName(serviceReceiverClass, env.getClass().getClassLoader()));

        String serializeClass = env.getProperty(PREFIX + ".serializeClass", String.class);
        if (StringUtils.isEmpty(serializeClass)) { serializeClass = "org.ala.tiktools.rpc.plugin.serializer.kryo.KryoThreadSerializer"; }
        c.setSerializeClass((Class<? extends ISerializer>) ClassUtils.forName(serializeClass, env.getClass().getClassLoader()));
        c.setSerializeDefSize(env.getProperty(PREFIX + ".serializeDefSize", Integer.class, 4096));
        c.setSerializePoolSize(env.getProperty(PREFIX + ".serializePoolSize", Integer.class, -1));
        c.setSerializeInPlace(env.getProperty(PREFIX + ".serializeInPlace", Boolean.class, false));
        
        String scanPkgs = env.getProperty(PREFIX + ".rpcClientPackages", String.class, "");
        if (!StringUtils.isEmpty(scanPkgs)) { c.setRpcClientPackages(scanPkgs.split(",")); }

        c.setReNodeInfoInterval(env.getProperty(PREFIX + ".reNodeInfoInterval", Long.class, 60000L));
        c.setReActiveInterval(env.getProperty(PREFIX + ".reActiveInterval", Long.class, 5000L));
        c.setReActiveMaxInterval(env.getProperty(PREFIX + ".reActiveMaxInterval", Long.class, 120000L));
        c.setRePopNodeInterval(env.getProperty(PREFIX + ".rePopNodeInterval", Long.class, 60000L));
        c.setTimeout(env.getProperty(PREFIX + ".timeout", Long.class, 6000L));
        c.setIdleInterval(env.getProperty(PREFIX + ".idleInterval", Long.class, 6000L));
        c.setIdleTimesClose(env.getProperty(PREFIX + ".idleTimesClose", Integer.class, 5));
        c.setServiceCoreSize(env.getProperty(PREFIX + ".serviceCoreSize", Integer.class, 20));
        c.setServiceMaxSize(env.getProperty(PREFIX + ".serviceMaxSize", Integer.class, 20));
        c.setServiceQueueSize(env.getProperty(PREFIX + ".serviceQueueSize", Integer.class, 1024));

        String chainsStr = env.getProperty(PREFIX + ".interceptorSenderChain", String.class);
        if (chainsStr != null) {
            String[] chains = chainsStr.split(",");
            c.setInterceptorSenderChain(Arrays.stream(chains).map(s -> s.trim()).collect(Collectors.toList()).toArray(new String[chains.length]));
        }
        
        chainsStr = env.getProperty(PREFIX + ".interceptorReceiverChain", String.class);
        if (!StringUtils.isEmpty(chainsStr)) {
        	String[] chains = chainsStr.split(",");
        	c.setInterceptorReceiverChain(Arrays.stream(chains).map(s -> s.trim()).collect(Collectors.toList()).toArray(new String[chains.length]));
        }
        
        RpcAuth auth = new RpcAuth();
        auth.setUsername(env.getProperty(PREFIX + ".auth.username", String.class));
        auth.setPassword(env.getProperty(PREFIX + ".auth.password", String.class));
        auth.setTimeout(env.getProperty(PREFIX + ".auth.timeout", Long.class, 6000L));
        c.setAuth(auth);
        return c;
    }


    /**
     *  解析扩展配置
     */
    public void parseExpand(Environment env) {}
}
